/**
 * SysAdmin class
 */
package docoffice.doss;

import java.sql.ResultSet;
import java.util.logging.Logger;

import docoffice.database.DatabaseManager;

/**
 * @author Ray Nosari
 * @version $Revision: 1.0 $
 */
public class SysAdmin extends MedicalEmployee{
	
	/**
	 * Field name
	 */
	private String name;
	
	/**
	 * Field LOG.
	 */
	private static final Logger LOG = Logger.getLogger("SysAdmin");
	
	/**
	 * SysAdmin constructor
	 * @param name
	 */
	public SysAdmin(String name){
		super(name, "SysAdmin");
		this.name = name;
	}
	
	/**
	 * name getter
	 * @return name
	 */
	public String getName(){
		return name;
	}
	
	/**
	 * name setter
	 * @param name
	 */
	public void setName(String name){
		this.name = name;
	}
	
	/**
	 * 
	 * @param username
	 */
	public void unlockUser(String username){
		final DatabaseManager dbMgr = new DatabaseManager();
		final String sqlQuery = "UPDATE login SET isLocked = 0 WHERE Username = '" 
			+ username + "'";
		
		try{
			dbMgr.runUpdateQuery(sqlQuery);
		} catch(Exception e){
			LOG.severe("A SQL error occurred while unlocking user account");
			e.printStackTrace();
		} finally{
			dbMgr.closeConnection();
		}
		LOG.info("User account unlocked");
	}
	
	/**
	 * Method lockUser.
	 * @param username String
	 */
	public void lockUser(String username){
		final DatabaseManager dbMgr = new DatabaseManager();
		final String sqlQuery = "UPDATE login SET isLocked = 1 WHERE Username = '" 
			+ username + "'";
		
		try{
			dbMgr.runUpdateQuery(sqlQuery);
		} catch(Exception e){
			LOG.severe("A SQL error occurred while locking user account");
			e.printStackTrace();
		} finally{
			dbMgr.closeConnection();
		}
		LOG.info("User account has been locked after 3 unsuccessful attempts");
	}
	
	/**
	 * Method isAccountLocked.
	 * @param username String
	 * @return boolean
	 */
	public boolean isAccountLocked(String username){
		boolean isLocked = false;
		final DatabaseManager dbMgr = new DatabaseManager();
		final String sqlQuery = "SELECT isLocked FROM login WHERE Username = '" + username + "'";
		ResultSet rs = null;
		
		try{
			rs = dbMgr.runReadQuery(sqlQuery);
			rs.first();
			isLocked = rs.getBoolean("isLocked");
		} catch(Exception e){
			e.printStackTrace();
		} finally{
			dbMgr.closeConnection(rs);
		}
		
		return isLocked;
	}
	
	/**
	 * overridden toString method
	 * @return ret
	 */
	public String toString(){
		final String ret = "SysAdmin has info about system administrators";
		return ret;
	}
}